package com.itheima.d05_thread_question;

/**
 * 账户类
 */
public class Account {

    private String cardId; //卡号
    private double money; //金额

    /**
     * 取钱方法
     * @param money
     */
    public void takeoutMoney(double   money){
        //获取当前线程对象
        Thread thread = Thread.currentThread();

        //1.判断余额是否充足
        //锁 java中的所有对象都可以作为一把锁
        synchronized ("zhangsan") {
            if(this.money   >=  money){
                System.out.println(thread.getName()+"，正在取钱... ...");
                this.money = this.money - money;

                //2.取钱：更新余额
                System.out.println(thread.getName()+"，取钱成功！余额："+ this.money);
            }else{
                System.out.println(thread.getName()+"，取钱失败！余额不足！");
            }
        }
    }



    public Account(String cardId, double money) {
        this.cardId = cardId;
        this.money = money;
    }


    public String getCardId() {
        return cardId;
    }

    public void setCardId(String cardId) {
        this.cardId = cardId;
    }

    public double getMoney() {
        return money;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "cardId='" + cardId + '\'' +
                ", money=" + money +
                '}';
    }
}
